package com.shilei.search.word.segmentation;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.BaseAnalysis;
import org.ansj.splitWord.analysis.IndexAnalysis;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.assertj.core.util.Sets;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * @author 石雷
 * shilei1@jiayuan.com
 * 2021/4/1/001 14:44
 */
@Component
public class Ansj_ implements WordSegmenterI {
    @Override
    public Map<String, String> segMore(String text) {
        Map<String, String> map = new HashMap<>();

        StringBuilder result = new StringBuilder();
        for(Term term : BaseAnalysis.parse(text)){
            result.append(term.getName()).append(" ");
        }
        map.put("BaseAnalysis", result.toString());

        result.setLength(0);
        for(Term term : ToAnalysis.parse(text)){
            result.append(term.getName()).append(" ");
        }
        map.put("ToAnalysis", result.toString());

        result.setLength(0);
        for(Term term : NlpAnalysis.parse(text)){
            result.append(term.getName()).append(" ");
        }
        map.put("NlpAnalysis", result.toString());

        result.setLength(0);
        for(Term term : IndexAnalysis.parse(text)){
            result.append(term.getName()).append(" ");
        }
        map.put("IndexAnalysis", result.toString());

        return map;
    }

    @Override
    public Set<String> segOne(String text) {
        StringBuilder result = new StringBuilder();
        for(Term term : BaseAnalysis.parse(text)){
            result.append(term.getName()).append(" ");
        }
        return Sets.newHashSet(Arrays.asList(result.toString().split(" ")));
    }

}
